<?php
// 本类由系统自动生成，仅供测试用途
class NodeAction extends CommAction {
	public function __construct(){
		parent::__construct();
        $menuid =!empty($_REQUEST['menuid'])?intval($_REQUEST['menuid']):'';
        $nav_submenu=$this->submenu($menuid,false,-1);
        $this->assign('nav_submenu',$nav_submenu);
	}

    /**
     * lujinfa
     * 2013-09-03
     * 菜单列表
     */
    public function index(){


		$moduleModel =  new Model('node');
		$result = $moduleModel->select();
		$array = array();
		foreach($result as $r) {
			$r['name'] = $r['name'];
			if($r['level']==1){
				$r['leveldes'] = '应用';
			}else if($r['level']==2){
				$r['leveldes'] = '控制器';
			}else if($r['level']==3){
				$r['leveldes'] = '方法';
			}
			$r['selected'] = $r['id'] == $_GET['parentid'] ? 'selected' : '';
			$r['str_manage'] = '<a href="__APP_PATH__index.php?g=Admin&m=Node&a=add&parentid='.$r['id'].'"  class="fancybox fancybox.iframe">'.'添加子菜单'.'</a>
			| <a href="__APP_PATH__index.php?g=Admin&m=Node&a=edit&id='.$r['id'].'" class="fancybox fancybox.iframe">'.'修改'.'</a>
			| <a href="javascript:confirmurl(\'__APP_PATH__index.php?g=Admin&m=Node&a=delete&id='.$r['id'].'\',\''.'确认要删除吗？'.'\')">'.'删除'.'</a> ';
			$array[] = $r;
		}
		$str  = "<tr>
					<td align='center'>\$id</td>
					<td align='left' >\$title</td>
					<td >\$leveldes</td>
					<td >\$name</td>
					<td >\$g</td>

					<td >\$m</td>
					<td >\$a</td>
					<td >\$data</td>
					<td >\$sort</td>


					<td align='center'>\$str_manage</td>
				</tr>";
        import("@.ORG.Util.Tree");
		$tree = new Tree();
		$tree->init($array);
		$select_categorys = $tree->get_tree(0, $str);
		$this->assign('select_categorys',$select_categorys);
		$this->display('list');
	}

    /**
     * lujinfa
     * 201319-03
     * 编辑菜单
     */
    public function edit(){
		$mouid = trim($_REQUEST['id']) ? trim($_REQUEST['id']) : 0;
		if(!$mouid){
			$this->error('请选择一个菜单进行修改','__URL__/index');
		}else{
			if($_POST['dosubmit']){
				$moduleModel = new Model('node');

                $module['name'] = !empty($_POST['name'])?trim($_POST['name']):'';
                $module['title'] = !empty($_POST['title'])?trim($_POST['title']):'';
                $module['url'] = !empty($_POST['url'])?trim($_POST['url']):'';

                $module['pid'] = !empty($_POST['parentid'])?trim($_POST['parentid']):'';
                $module['level'] = !empty($_POST['level'])?trim($_POST['level']):'';
                $module['status'] = !empty($_POST['level'])?trim($_POST['display']):'';
                $module['g'] = !empty($_POST['Qg'])?trim($_POST['Qg']):'Admin';
                $module['m'] = !empty($_POST['Qm'])?trim($_POST['Qm']):'';
                $module['a'] = !empty($_POST['Qa'])?trim($_POST['Qa']):'';
                $module['data'] = !empty($_POST['data'])?trim($_POST['data']):'';
                $module['sort'] = !empty($_POST['sort'])?trim($_POST['sort']):'0';
                $module['type'] = !empty($_POST['type'])?trim($_POST['type']):'0';


                if($module['name']=='')
                {
                    $this->error('操作名称不能为空','__APP_PATH__index.php?g=Admin&m=Node&a=add');
                }

                if($module['title']=='')
                {
                    $this->error('中文名称不能为空','__APP_PATH__index.php?g=Admin&m=Node&a=add');
                }


				$condition['id'] = $mouid;
				if($moduleModel->where($condition)->save($module)){
					$this->success('更新成功','__APP_PATH__index.php?g=Admin&m=Node&a=index');
				}else{
					$this->error('更新失败','__APP_PATH__index.php?g=Admin&m=Node&a=edit&id='.$mouid);
				}
			}else{
				$moduleModel = new Model('node');
				$condition['id'] = $mouid;
				$editModule = $moduleModel->where($condition)->find();
				if($editModule){
					$result = $moduleModel->select();
					$array = array();
					foreach($result as $r) {
						$r['cname'] = $r['title'];
						$r['selected'] = $r['id'] == $editModule['pid'] ? 'selected' : '';
						$array[] = $r;
					}
					$str  = "<option value='\$id' \$selected>\$spacer \$cname</option>";
                    import("@.ORG.Util.Tree");
					$tree = new Tree();
					$tree->init($array);
					$select_categorys = $tree->get_tree(0, $str);
					$this->assign('editModule',$editModule);
					$this->assign('select_categorys',$select_categorys);
					$this->display('edit');
				}else{
					$this->error('菜单不存在，请重新选择','__APP_PATH__index.php?g=Admin&m=Node&a=list');
				}
			}
		}

	}

    /**
     * lujinfa
     * 2013-09-04
     * 删除节点
     */
    public function delete(){
		$mouid = trim($_REQUEST['id']) ? trim($_REQUEST['id']) : 0;
		if(!$mouid){
			$this->error('请选择一个菜单进行删除','__URL__/menulist');
		}else{
			$moduleModel = new Model('node');
			$condition['id'] = $mouid;
			$keyid = $moduleModel->where($condition)->getField('id');
			if(!empty($keyid)){
				$condit['pid'] = $mouid;
				$resultList = $moduleModel->where($condit)->select();
				if($resultList){
					$this->error('该菜单下存在子菜单，请先删除子菜单','__URL__/menulist');
				}else{
					if($moduleModel->where($condition)->delete()){
						$this->success('删除成功','__URL__/menulist');
					}else{
						$this->error('删除失败','__URL__/menulist');
					}
				}
			}else{
				$this->error('删除菜单不存在','__URL__/menulist');
			}
		}
	}

    /**
     * lujinfa
     * 2013-09-03
     * 添加菜单
     */
    public function add(){

		if($_POST['dosubmit']){
			$moduleModel =  new Model('node');
			;
			$module['name'] = !empty($_POST['name'])?trim($_POST['name']):'';
			$module['title'] = !empty($_POST['title'])?trim($_POST['title']):'';
			$module['url'] = !empty($_POST['url'])?trim($_POST['url']):'';

			$module['pid'] = !empty($_POST['parentid'])?trim($_POST['parentid']):'';
			$module['level'] = !empty($_POST['level'])?trim($_POST['level']):'';
			$module['status'] = !empty($_POST['level'])?trim($_POST['display']):'';
			$module['g'] = !empty($_POST['Qg'])?trim($_POST['Qg']):'Admin';
			$module['m'] = !empty($_POST['Qm'])?trim($_POST['Qm']):'';
			$module['a'] = !empty($_POST['Qa'])?trim($_POST['Qa']):'';
			$module['data'] = !empty($_POST['data'])?trim($_POST['data']):'';
			$module['sort'] = !empty($_POST['sort'])?trim($_POST['sort']):'0';

            $module['type'] = !empty($_POST['type'])?trim($_POST['type']):'0';
            if($module['name']=='')
            {
                $this->error('操作名称不能为空','__APP_PATH__index.php?g=Admin&m=Node&a=add');
            }

            if($module['title']=='')
            {
                $this->error('中文名称不能为空','__APP_PATH__index.php?g=Admin&m=Node&a=add');
            }

           //$module['remark'] = !empty($_POST['moduledesc'])?trim($_POST['moduledesc']):'';




			if($op=$moduleModel->create($module)){
				if($moduleModel->add($module)){
					$this->success('添加成功','__APP_PATH__index.php?g=Admin&m=Node&a=index');
				}else{
					$this->error('添加失败，请重新添加','__APP_PATH__index.php?g=Admin&m=Node&a=add');
				}
			}else{
				$this->error($moduleModel->getError());
			}
		}else{

			$moduleModel =  new Model('node');
			$parentid = trim($_REQUEST['parentid']) ? trim($_REQUEST['parentid']) : 0;
			$result = $moduleModel->select();
			$array = array();
			foreach($result as $r) {
				//$r['name'] ;
				$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
				$array[] = $r;
			}
			$str  = "<option value='\$id' \$selected>\$title</option>";
            import("@.ORG.Util.Tree");

			$tree = new Tree();
			$tree->init($array);
			$select_categorys = $tree->get_tree(0, $str);
			$this->assign('select_categorys',$select_categorys);
			$this->display('add');
		}
	}
}